<?php
/**
 * 微信推送信息
 */

namespace App\Controller\Index;


use Common\Model\MemberOtherPlatformModel;
use Think\Controller;

class MsgPushController extends Controller
{
    /**
     * 帐号配置
     * @var
     */
    private $accountsConf = [
        //洋玩易  正式账号  洋玩易平台
        'gh_bfc025005d01' => [   //gh_bfc025005d01 新洋玩意
            'appid' => 'wxac2a54fc8d293824',   //新洋玩易  wxac2a54fc8d293824 wx88c66a1811791b74
            'appsecret' => 'c3e737dbfa93c2fa3a0beb3523680d87',   //新洋玩易  c3e737dbfa93c2fa3a0beb3523680d87
            'msgtpl' => [
                // 开店成功通知
                'openShopSuccess' => [
                    'id' => '-7fPGxXBY3soOJnYdPNrASErYi0gIZnY4xlgAZ2y7MA',
                    'params' => ['first','keyword1','keyword2','remark']
                ],
                // 订单已发货通知
                'orderShipped' => [
                    'id' => '9IjX3krjCVCPECLhVjqlyY_FkOaDmOBo3RV0v0ht6m0',
                    'params' => ['first','keyword1','keyword2','keyword3','keyword4','remark']
                ],
                // 店铺新订单成交通知
                'shopOrderSuccess' => [
                    'id' => 'TS8A6UkrhCNTboXx4Qg6Q5klz3_YGWvoryHq6rC7o1k',
                    'params' => ['first','keyword1','keyword2','keyword3','keyword4','remark']
                ],
                // 提现提交通知
                'withdrawSubmit' => [
                    'id' => 'Lu_h4w7m5U7x3D5QAJLRd6beX7yIP-T2oGkIUiciHn4',
                    'params' => ['first','money','timet','remark']
                ],
                // 提现失败通知
                'withdrawFaild' => [
                    'id' => 'TkoKvByLv-ySvd4-07mkdmWkwEZire-iNFPSvfymz0E',
                    'params' => ['first','money','time','remark']
                ],
                // 提现成功通知
                'withdrawSuccess' => [
                    'id' => 'yf2neZWEoA-UO5TOzD_39YcMZs5EjLGQqZfBdSh9d1A',
                    'params' => ['first','money','timet','remark']
                ],
                // 福利到帐提醒
                'bonusRemind' => [
                    'id' => '9Teog1ZSRPOAdd7dRg3GTYioWXO23_8W463h06Zz6SE',
                    'params' => ['first','keyword1','keyword2','remark']
                ],

                //上级订单收益提醒
                'parentOrderSuccess' => [
                    'id' => 'wedB8Ict7TdyHQqztx9jjENLRs8oiEWjK6oDcw4EN1w',
                    'params' => ['first','keyword1','keyword2','remark']
                ],
                // 付款成功通知
                'orderPaySuccess' => [
                    'id' => '2_EGomdMdywC3Jhp9ycY_wRAJk0BOVhGUxJCaOOf3OM',
                    'params' => ['first','orderProductPrice','orderProductName','orderAddress','orderName','remark']
                ],
                //给上级店铺推送订单消息
                'shopOrderSuceess' => [
                    'id' => '-8JMyQ1BdFjXB8WZ8FofAQYerpwAsRUWOX-XA_f5L30',
                    'params' => ['first','keyword1','keyword2','keyword3','remark']
                ],
                // 店铺升级成功通知--成为合伙人
                'shopUpgradeSuccess' => [
                    'id' => 'PaBFoZL2_8hOst7cA2kb-iF_ZSfXsx_y-aIJ075Rung',
                    'params' => ['first','keyword1','keyword2','remark']
                ],
                //拼团成功通知
                'pinTuanSuccess' => [
                    'id' => 'V9vWQ2uIXXYW34DDcKB5PcL6fIMbJycbYIEAZkNS9go',
                    'params' => ['first','keyword1','keyword2','keyword3','remark']
                ],
                //推荐奖励通知
                'tuijianSuccess' => [
                    'id' => 'HRL2pyZnIRcIf31wmhEbRz4zjgNUm_0FAAmDHBw1CS8',
                    'params' => ['first','keyword1','keyword2','keyword3','remark']
                ],
                //膜法伊人订单成功通知
                'magicSuccess' => [
                    'id' => 'iYnLv5PCeqCuljJOFv0VLiCZ21KrMsEE8Uzzfplb0g0',
                    'params' => ['first','keyword1','keyword2','keyword3','keyword4','remark']
                ],
                //新用户入驻通知
                'newUserInfoSuceess' => [
                    'id' => 'tghBgko5E5d0wub2zqm5IvqB0J1-yxv4d_NOGRt7cg4',
                    'params' => ['first','keyword1','keyword2','remark']
                ]

            ]
        ],

        'ywy_oline' => [   //gh_bfc025005d01 新洋玩意  gh_1cebae3dcfc7  点点拿
            'appid' => 'wxac2a54fc8d293824',   //新洋玩易  wxac2a54fc8d293824
            'appsecret' => 'c3e737dbfa93c2fa3a0beb3523680d87'   //新洋玩易  c3e737dbfa93c2fa3a0beb3523680d87
        ]
    ];
    // 消息推送接口
    private $wxTplApi = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s';

    public function msgPush($memberId, $type, $json){
        $weixin = 'gh_bfc025005d01';
        $wxtpl =$this->accountsConf[$weixin]['msgtpl'][$type];
        $appid = $this->accountsConf[$weixin]['appid'];
        $secret = $this->accountsConf[$weixin]['appsecret'];
        $token = $this->getAccessTokens($appid,$secret);
        $api = sprintf($this->wxTplApi, $token);
        $tplId = $wxtpl['id'];
        $tplData = $this->getTplParam($wxtpl['params'], $json);
        $jsonArr = json_decode($json, true);
        $url = isset($jsonArr['url']) ? $jsonArr['url'] : '';
        $model = new MemberOtherPlatformModel();
        $openId = $model->getWeixinOpenIdByMemberId($memberId);
        $flg=(string)$openId;
        $data = [
            "touser" => $flg,
            "template_id" => $tplId,
            "url" => $url,
            "data" => $tplData
        ];
        $res = curl_file_get_contents($api, json_encode($data));
        $res = json_decode($res, true);
        return $res;
    }
    public function getAccessTokens($appid,$secret){
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $secret;
        $tempArr = json_decode($this->httpGet($url));
        if (@array_key_exists ( 'access_token', $tempArr )) {
            $access_token = $tempArr->access_token;
            return $access_token;
        } else {
            return 0;
        }
    }
    public function httpGet($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }

    protected function getTplParam($params, $json){
        $data = json_decode($json, true);
        $return  = [];
        foreach ($params as $param){
            isset($data[$param]) && $return[$param] = ['value'=>$data[$param]];
        }
        return $return;
    }
}